package com.informatics.polymer.client.ontology;

import org.restlet.gwt.Callback;
import org.restlet.gwt.Client;
import org.restlet.gwt.data.Form;
import org.restlet.gwt.data.Protocol;
import org.restlet.gwt.data.Request;
import org.restlet.gwt.data.Response;
import org.restlet.gwt.resource.Representation;

import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeGrid;
/**
 * Class to create ontology panel.
 * @author ed
 * @version 1.0
 */
public class OntologyPanel extends SectionStack{
	private TreeGrid propertyGrid;

	public OntologyPanel(){
		setUpPanel();
	}

	private void setUpPanel() {
		this.setVisibilityMode(VisibilityMode.MULTIPLE);
		this.setWidth(1000);
		this.setHeight(500);
		this.setBorder("2px solid #458B00");
		SectionStackSection section1 = new SectionStackSection("Ontology");
		section1.setExpanded(true);
		HTMLFlow welcome = new HTMLFlow();
		welcome.setOverflow(Overflow.AUTO);
		welcome.setPadding(10);
		String welcomeString = "Welcome to the <I>\"Ontology\"</I> panel, please see the instructions tab "
				+ "below for more details on how to browse the properties, techniques and conditions ontologies.";
		welcome.setContents(welcomeString);
		section1.addItem(welcome);
		this.addSection(section1);
		this.setStyleName("sectionHeaderclosed");
		this.setBackgroundColor("#F0F0F0");
		//Convert OWL to XML.
		SectionStackSection OWL2XML = new SectionStackSection("OWL2XML Converter");
		Button convertPropertyOWL2XMLButton = new Button("Convert OWL to XML");
		convertPropertyOWL2XMLButton.setSize("200", "30");
		final DynamicForm form = new DynamicForm();
		form.setWidth(250);
		VLayout ontologyLayout = new VLayout();
		final TextItem ontologyName = new TextItem();  
		ontologyName.setTitle("Ontology Name"); 
		ontologyName.setRequired(true); 
		final TextItem ontologyURI = new TextItem();  
		ontologyURI.setTitle("Ontology URI"); 
		ontologyURI.setRequired(true); 
		form.setFields(ontologyName,ontologyURI);
		convertPropertyOWL2XMLButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				final Client client = new Client(Protocol.HTTP);
				Form form = new Form();
				form.add("OntologyURI", ontologyURI.getDisplayValue());
				form.add("OntologyName1", ontologyName.getDisplayValue());
				Representation rep = form.getWebRepresentation();
				client.post("http://localhost:8080/svc/owl2xml", rep, new Callback() {
					@Override
					public void onEvent(Request request, Response response) {
					}});

			}
		});
		ontologyLayout.addMember(form);
		ontologyLayout.addMember(convertPropertyOWL2XMLButton);
		OWL2XML.addItem(ontologyLayout);
		this.addSection(OWL2XML);
		// Add properties ontology
		SectionStackSection propertySection = new SectionStackSection("Properties");
		setPropertyGrid(new TreeGrid());
		this.addSection(propertySection);
		// Add technique ontology
		SectionStackSection techniquesSection = new SectionStackSection(
				"Techniques");
		this.addSection(techniquesSection);
		// Add measurement conditions ontology.
		SectionStackSection conditionsSection =  new SectionStackSection("Conditions");
		this.addSection(conditionsSection);
		// Add instructions section.
		SectionStackSection instructionSection = new SectionStackSection(
				"Instructions");
		instructionSection.setExpanded(false);
		HTMLFlow browseRepeatUnitInstructions = new HTMLFlow();
		browseRepeatUnitInstructions.setOverflow(Overflow.AUTO);
		browseRepeatUnitInstructions.setPadding(10);
		String instructions = "1. <b>Using the Ontology panel.</b><br></br> Click on one of the panels to view the ontology.<br></br>"
				+ "2. <b>Delete Panel</b><br></br> If you are not happy with your selection or choose to view another panel you can click on the <I>\"Delete Panel\"</I> and press the delete panel button"
				+ " this will delete the ontology panel, so you can start over again.<br></br>";
		browseRepeatUnitInstructions.setContents(instructions);
		instructionSection.addItem(browseRepeatUnitInstructions);
		this.addSection(instructionSection);
		// Delete panel section
		SectionStackSection deletePanelSection = new SectionStackSection(
				"Delete Panel");
		deletePanelSection.setExpanded(false);
		Button deletePanelButton = new Button("Delete Panel!");
		deletePanelButton.setSize("100", "30");
		deletePanelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				destroy();
			}
		});
		deletePanelSection.addItem(deletePanelButton);
		this.addSection(deletePanelSection);

	}

	/**
	 * @param propertyGrid the propertyGrid to set
	 */
	public void setPropertyGrid(final TreeGrid propertyGrid) {
		this.propertyGrid = propertyGrid;
	}

	/**
	 * @return the propertyGrid
	 */
	public TreeGrid getPropertyGrid() {
		return propertyGrid;
	}
}
